//
//  SelectJobTypeView.swift
//  GreenDeal
//
//  Created by Moennig Q on 2023/8/24.
//

import UIKit
import SnapKit
import RxCocoa
import RxSwift
import RxGesture
class CreateJobSelectJobTypeView: CreateJobBaseView {
    var selectJobType: JobType = .pvdJob
    let pvdTypeImageView = CreateJobTypeView(title: JobType.pvdJob.rawValue)
    let swhTypeImageView = CreateJobTypeView(title: JobType.swhJob.rawValue)
    override func setUpContentView() {
        //pvdTypeImageView
        contentView.addSubview(pvdTypeImageView)
        pvdTypeImageView.snp.makeConstraints { make in
            make.top.equalTo(30)
            make.left.equalTo(20)
            make.right.equalTo(-20)
            make.height.greaterThanOrEqualTo(0)
        }
        //swhTypeImageView
        contentView.addSubview(swhTypeImageView)
        swhTypeImageView.snp.makeConstraints { make in
            make.top.equalTo(pvdTypeImageView.snp.bottom).offset(20)
            make.left.right.equalTo(pvdTypeImageView)
            make.height.greaterThanOrEqualTo(0)
        }
        //选择pvd job type
//        pvdTypeImageView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
//            guard let strongSelf = self else { return  }
//            if strongSelf.selectJobType == .swhJob{
//                strongSelf.selectPVDJobType()
//            }
//        }).disposed(by: self.disposeBag)
        //选择swh job type
//        swhTypeImageView.rx.tapGesture().when(.recognized).subscribe(onNext: { [weak self] _ in
//            guard let strongSelf = self else { return  }
//            if strongSelf.selectJobType == .pvdJob{
//                strongSelf.selectSWHJobType()
//            }
//        }).disposed(by: self.disposeBag)
        //swh没有创建功能，暂时不能选择
        swhTypeImageView.disabledJobType(imageName: "createjob_select_swh_jobtype_disabled")
    }
    //MARK: - 选择PVD job type
    func selectPVDJobType(){
        self.pvdTypeImageView.setSelect(imageName: "createjob_select_pvd_jobtype_sel")
        self.selectJobType = .pvdJob
    }
    //MARK: - 选择SWH job type
//    func selectSWHJobType(){
//        self.pvdTypeImageView.setUnSelect()
//        self.swhTypeImageView.setSelect()
//        self.selectJobType = .swhJob
//    }
}
class CreateJobTypeView: UIView{
    let imageView = UIImageView()
    let titleLabel = ContentLabel(R.font.interBold(size: 18))
    init(title: String) {
        super.init(frame: CGRect.zero)
        self.titleLabel.text = title
        self.setUpView()
    }
    //IB init
    required init?(coder aDecoder: NSCoder) {
        super.init(coder:aDecoder)
    }
    //纯代码创建
    override init(frame: CGRect) {
        super.init(frame: frame)
        self.setUpView()
    }
    private func setUpView() {
        self.layer.cornerRadius = 10
        self.clipsToBounds = true
        imageView.contentMode = .scaleAspectFit
        //job type view
        self.addSubview(imageView)
        imageView.snp.makeConstraints { make in
            make.top.equalTo(24)
            if APPConfig.WidthForPortrait == 320 {//兼容4寸设备
                make.height.equalTo(78)
                make.width.equalTo(100)
            }else{
                make.height.equalTo(156)
                make.width.equalTo(200)
            }
            make.centerX.equalToSuperview()
        }
        //title label
        self.addSubview(titleLabel)
        titleLabel.snp.makeConstraints { make in
            make.top.equalTo(imageView.snp.bottom).offset(16)
            make.width.height.greaterThanOrEqualTo(0)
            make.centerX.equalToSuperview()
            make.bottom.equalTo(-24)
        }
    }
    //选中
    func setSelect(imageName: String){
        self.imageView.image = UIImage(named: imageName)
        self.backgroundColor = UIColor(hexStr: "#005D2D1A")
        self.titleLabel.textColor = UIColor(hexStr: Constant.ThemeColorHexStr)
        self.layer.borderColor = UIColor(hexStr: Constant.ThemeColorHexStr).cgColor
        self.layer.borderWidth = 1
    }
    func setUnSelect(imageName: String){
        self.imageView.image = UIImage(named: imageName)
        self.backgroundColor = UIColor(hexStr: "#F5F5F5")
        self.titleLabel.textColor = UIColor(hexStr: "#292D32")
        self.layer.borderColor = UIColor.clear.cgColor
        self.layer.borderWidth = 0
    }
    func disabledJobType(imageName: String){
        self.imageView.image = UIImage(named: imageName)
        self.backgroundColor = UIColor(hexStr: "#F5F5F5")
        self.titleLabel.textColor = UIColor(hexStr: "#292D3266")
        self.layer.borderColor = UIColor.clear.cgColor
        self.layer.borderWidth = 0
    }
}
